import {Toast} from 'mint-ui'
/*设置 html 根字体为 rem*/
export function setRem(){
  function setWidth() {
    let oWidth = document.documentElement.clientWidth||document.body.clientWidth;
    document.getElementsByTagName("html")[0].style.fontSize = oWidth+'px'
  }
  setWidth();
  window.onresize = function(){
    setWidth();
  };
}
/**
 * Md5 密码加密
 * @param text 密码
 * @returns {*}
 */
export function md(text) {
  let md5=require('md5');
  let myText=text.toString();
  let firstStr=btoa(text.toString().substr(0,3));
  let twoStr=btoa(text.toString().substr(3));
  return md5(twoStr+firstStr);
}
/*验证 错误信息*/
export function validMsg(){

  return {
    "validReg":{
      "phoneReg":/^0?(13[0-9]|145|147|149|15[0-3]|15[5-9]|166|17[1-3]|17[5-8]|18[0-9]|198|199)[0-9]{8}$/,
      "smsTime":10,
      "smsBtnText":"重新获取验证码",
      "getSmsBtnText":"秒后重发",
    },
    "message":{
      "noPhone":"请输入11位手机号码",
      "errPhone":"手机号码格式错误",
      "noPicCode":"请输入图片验证码",
      "errPicCode":"图片验证码错误或已过期",
      "noSmsCode":"请输入验证码",
      "errSmsCode":"验证码错误或已过期",
      "noPassword":"请输入密码",
      "errPassword":"密码长度最少6位",
      "noConfirmPwd":"请再次输入密码",
      "errConfirmPwd":"两次密码不一致"
    }

  }


}

/**
 * 倒计时
 * @self this
 *
 */
export function timer(self) {
   self.timers=setInterval(()=>{
    self.time--;
    if(self.time<=0){
     initBtnVal(self);
      clearInterval(self.timers)
    }else {
      self.isCalculation=true;
      self.timeNum= self.time+validMsg().validReg.getSmsBtnText;
    }
  },1000);

}
/**
 * 初始获取验证码 按钮 值
 * @param self
 */
export function initBtnVal(self) {
  self.isCalculation=false;
  self.timeNum=validMsg().validReg.smsBtnText;
}

/**
 * 错误提示弹出框
 * @param msg 错误信息
 */
 export function falseHints(msg) {
   Toast({
     message: msg,
     position: 'bottom',
     duration: 2000
   });
 }

/**
 * 获取错误信息
 * @param errorMsg 错误信息 以‘,’拼接的错误信息
 */
export function getErrMsg(errorMsg) {
  let errorArr = errorMsg.split(",");
  let needErrArr = [];
  for (let i = 0; i < errorArr.length; i++) {
    if (errorArr[i] !== "undefined") {
      needErrArr.push(errorArr[i])
    }
  }
  return needErrArr;
}
